//
//  TabBarController.swift
//  FuXingApp
//
//  Created by hungraim on 2021/8/2.
//

import UIKit

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        baseSetting()
        
//        FXDynamicConfigurationJSONManager.addJsonRefreshObserver(self)
        
        addChildVCs()
    }
    
    
    /// 基础设置
    fileprivate func baseSetting() {
        let item = UITabBarItem.appearance(whenContainedInInstancesOf: [type(of: self)])
        let bar = UITabBar.appearance(whenContainedInInstancesOf: [type(of: self)])
        // 设置文字属性
        var attrs: [NSAttributedString.Key : Any] = [:]
        attrs[NSAttributedString.Key.font] = UIFont.systemFont(ofSize:10)
        attrs[NSAttributedString.Key.foregroundColor] = UIColor(hex: "B4BCCF")
        
        var selectAttrs: [NSAttributedString.Key : Any] = [:]
        selectAttrs[NSAttributedString.Key.font] = attrs[NSAttributedString.Key.font]
        selectAttrs[NSAttributedString.Key.foregroundColor] = UIColor.appTint
        
        item.setTitleTextAttributes(attrs, for: .normal)
        item.setTitleTextAttributes(selectAttrs, for: .selected)
        
        // 去掉透明后导航栏下边的黑边
        if #available(iOS 13.0, *) {
//            bar.backgroundImage = UIImage(color: UIColor.clear)
//            bar.shadowImage = UIImage(color: UIColor.clear)
            bar.backgroundImage = UIImage()
            bar.shadowImage = UIImage()
        } else {
            bar.backgroundImage = UIImage()
            bar.shadowImage = UIImage()
        }
        bar.isTranslucent = false
        
    }

}

// MARK: - FXConfigurationJSONRefreshDelegate
//extension TabBarController: FXConfigurationJSONRefreshDelegate {
//    func configurationJSONDidFresh(_ jsonModel: DynamicConfigurationJSONModel!) {
//        for item in jsonModel.otherPara.tabBar {
//            RouterManager.shared.searchControllerInMap(key: item.tabView) { controllerName, info in
//                let className = controllerName ?? "WebViewController"
//
//                let namespace = Bundle.main.infoDictionary!["CFBundleName"] as! String
//
//                let classFromStr: AnyClass? = NSClassFromString(namespace + "." + className)
//
//                let viewControllerClass = classFromStr as? UIViewController.Type
//                guard let CClass = viewControllerClass else {
//                    return
//                }
//                // 创建控制器对象
//                let childVc = CClass.init()
//                setUpChildVc(childVc, title: item.text, image: "", selectImage: "")
//            }
//        }
//    }
//    
//}

// MARK: - 添加子控制器
extension TabBarController {
    func addChildVCs() {
//        let homeVC = HomeController()
//        setUpChildVc(homeVC, title: "首页", image: "icon_tabbar_home", selectImage: "icon_tabbar_home_pre")
//        let fuliVC = WelfareController()
//        setUpChildVc(fuliVC, title: "福利", image: "icon_tabbar_fuli", selectImage: "icon_tabbar_fuli_pre")
//        
//        let storeVC = StoreViewController()
//        setUpChildVc(storeVC, title: "商城", image: "icon_tabbar_store", selectImage: "icon_tabbar_store_pre")
//        
//        let meVC = MeController()
//        setUpChildVc(meVC, title: "我的", image: "icon_tabbar_me", selectImage: "icon_tabbar_me_pre")
    }
    
    func setUpChildVc(_ vc: UIViewController, title: String, image: String, selectImage: String) {
        // 设置文字和图片
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: selectImage)?.withRenderingMode(.alwaysOriginal)
        if #available(iOS 15.0, *) {
            let attributes = [NSAttributedString.Key.foregroundColor: UIColor.lightGray] as [NSAttributedString.Key : Any]
            let selectAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue] as [NSAttributedString.Key : Any]
            let appearance = UITabBarAppearance()
            appearance.shadowColor = .clear
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = attributes
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectAttributes
            self.tabBar.standardAppearance = appearance;
        }
        // 包装导航
        let nav = NavigationController(rootViewController: vc)
        vc.title = title
        
        self.addChild(nav)
        
    }
}
